package com.luo.springboot_luo.service.Impl;

import com.luo.springboot_luo.mapper.UserMapper;
import com.luo.springboot_luo.pojo.User;
import com.luo.springboot_luo.pojo.dto.UserDTO;
import com.luo.springboot_luo.service.UserService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service //配置为一个Spring的Service组件 bean
public class UserServiceImpl implements UserService {
    private final UserMapper userMapper;

    @Autowired
    public UserServiceImpl(UserMapper userMapper) {
        this.userMapper = userMapper;
    }

    @Override
    public User addUser(UserDTO user) {
        User userPojo = new User();
        BeanUtils.copyProperties(user, userPojo);
        return userMapper.save(userPojo);
    }

    @Override
    public User getUser(Integer userId) {
        return userMapper.findById(userId).orElseThrow(() -> {
            throw new RuntimeException("用户不存在");
        });
    }

    @Override
    public User setUser(UserDTO user) {
        User userPojo = new User();
        BeanUtils.copyProperties(user, userPojo);
        return userMapper.save(userPojo);
    }

    @Override
    public void deleteUser(Integer userId) {
        userMapper.findById(userId);
    }
}
